package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.EquipmentAttr;
import com.mt.roll.core.dal.entity.Shelf;
import com.mt.roll.core.dal.service.EquipmentAttrServiceBiz;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.EquipmentAttrVoMapper;
import com.mt.roll.wms.params.EquipmentAttrParam;
import com.mt.roll.wms.params.EquipmentAttrQueryParam;
import com.mt.roll.wms.service.EquipmentAttrService;
import com.mt.roll.wms.vo.EquipmentAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther yanyang
 * @date 2022/1/13 16:59
 */

@Service
@Transactional
public class EquipmentAttrServiceImpl extends BaseService implements EquipmentAttrService {

    @Autowired
    private EquipmentAttrServiceBiz equipmentAttrServiceBiz;

    @Autowired
    private EquipmentAttrVoMapper equipmentAttrVoMapper;

    @Override
    public R<PageVo<EquipmentAttrVo>> attr(EquipmentAttrQueryParam equipmentAttrQueryParam) {
        IPage<EquipmentAttrVo> page = equipmentAttrVoMapper.attr(new Page<>(equipmentAttrQueryParam.getCurrent(),equipmentAttrQueryParam.getSize()), equipmentAttrQueryParam);
        return successful(new PageVo<>(page, EquipmentAttrVo.class));
    }

    @Override
    public R<EquipmentAttrVo> attrById(IdParam idParam) {
        EquipmentAttr equipmentAttr = equipmentAttrServiceBiz.getById(idParam.getId());
        Assert.notNull("详情不存在", equipmentAttr);
        EquipmentAttrVo equipmentAttrVo = equipmentAttrVoMapper.attrById(idParam.getId());
        return successful(equipmentAttrVo);
    }

    @Override
    public R<IdVo> addAttr(EquipmentAttrParam equipmentAttrParam) {
        QueryWrapper<EquipmentAttr> wrapper = new QueryWrapper<>();
        wrapper.eq(EquipmentAttr.ATTR_NAME, equipmentAttrParam.getAttrName());
        Assert.eqZero(equipmentAttrServiceBiz.count(wrapper), "该设备属性已经存在，请勿重复添加");
        EquipmentAttr equipmentAttr = new EquipmentAttr();
        BeanUtils.copyProperties(equipmentAttrParam, equipmentAttr);
        setCommonField(equipmentAttr);
        equipmentAttrServiceBiz.save(equipmentAttr);
        return successful(IdVo.builder().id(equipmentAttr.getId()).build());
    }

    @Override
    public R<IdVo> updateAttr(EquipmentAttrParam equipmentAttrParam) {
        EquipmentAttr equipmentAttr = equipmentAttrServiceBiz.getById(equipmentAttrParam.getId());
        //修改信息需要检验是否已存在
        if (!equipmentAttr.getAttrName().equals(equipmentAttrParam.getAttrName())) {
            QueryWrapper<EquipmentAttr> wrapper = new QueryWrapper<>();
            wrapper.eq(EquipmentAttr.ATTR_NAME, equipmentAttrParam.getAttrName());
            Assert.eqZero(equipmentAttrServiceBiz.count(wrapper),"该设备属性已经存在");
        }
        EquipmentAttr updateEquipmentAttr = new EquipmentAttr();
        BeanUtils.copyProperties(equipmentAttrParam, updateEquipmentAttr);
        setUpdateCommonField(updateEquipmentAttr);
        equipmentAttrServiceBiz.updateById(updateEquipmentAttr);
        return successful(IdVo.builder().id(equipmentAttr.getId()).build());
    }

    @Override
    public R<IdVo> deleteAttr(IdParam idParam) {
        EquipmentAttr equipmentAttr = equipmentAttrServiceBiz.getById(idParam.getId());
        if (equipmentAttr == null) {
            return failed("设备属性不存在");
        }
        equipmentAttrServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(equipmentAttr.getId()).build());
    }
}
